package com.se.frontline.layers;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;

import android.util.Log;
import android.widget.Toast;

import com.se.frontline.MainActivity;
import com.se.frontline.gameplay.ManagedGameScene;
import com.se.frontline.gameplay.heroes.Hero;
import com.se.frontline.gameplay.heroes.Hero1;
import com.se.frontline.gameplay.units.Unit;
import com.se.frontline.gameplay.units.Unit1;
import com.se.frontline.layers.HUDs.Joystick;
import com.se.frontline.managers.GameManager;
import com.se.frontline.managers.ResourceManager;
import com.se.frontline.layers.menu.FinishedMenuLayer;
import com.se.frontline.layers.menu.MainMenuLayer;

public class FrontlineHUD {

	private static final float JOYSTICK_MARGIN_LEFT = 10;

	private static final int JOYSTICK_MARGIN_BOTTOM = 10;

	private static final float HUD_TOP_BORDER = 0.65f * ResourceManager
			.getInstance().cameraHeight;

	private static final float SELECTION_SPRITES_WIDTH = 0.1f * ResourceManager
			.getInstance().cameraHeight;

	private static final float SELECTION_SPRITES_HEIGHT = 0.172f * ResourceManager
			.getInstance().cameraHeight;

	private static final float SELECTION_MARGIN_Y = 0.02f * ResourceManager
			.getInstance().cameraHeight;

	private static final float SELECTION_MARGIN_X = 0.013f * ResourceManager
			.getInstance().cameraWidth;

	private static final float RIGHT_MARGIN = 1f * ResourceManager
			.getInstance().cameraWidth - 10;

	private static final float SELECTION_SPRITES_ROW_1_Y = HUD_TOP_BORDER
			+ SELECTION_MARGIN_Y;

	private static final float SELECTION_SPRITES_ROW_2_Y = HUD_TOP_BORDER
			+ SELECTION_SPRITES_HEIGHT + SELECTION_MARGIN_Y;

	private static final float SELECTION_SPRITES_COLUMN_1_X = 0.3f
			* ResourceManager.getInstance().cameraWidth + SELECTION_MARGIN_X;

	private static final float SELECTION_SPRITES_COLUMN_2_X = 0.3f
			* ResourceManager.getInstance().cameraWidth
			+ SELECTION_SPRITES_WIDTH + SELECTION_MARGIN_X * 2;

	private static final float SELECTION_SPRITES_COLUMN_3_X = 0.3f
			* ResourceManager.getInstance().cameraWidth
			+ SELECTION_SPRITES_WIDTH * 2 + SELECTION_MARGIN_X * 3;

	// ====================================================
	// VARIABLES
	// ====================================================
	// the HUD for the actual game
	public static HUD HUDRegion = new HUD();

	private Joystick mJoystick;

	private ButtonSprite pauseButton;
	private ButtonSprite fightButton;

	// Sprite for the unit selection
	private TiledSprite Unit1SelectionSprite;
	private TiledSprite Unit2SelectionSprite;
	private TiledSprite Unit3SelectionSprite;
	private TiledSprite Unit4SelectionSprite;
	private TiledSprite Unit5SelectionSprite;
	private TiledSprite Unit6SelectionSprite;

	// Rectangle for the FoodBar
	public static Rectangle InnerFoodBar;

	// ====================================================
	// CONSTRUCTOR
	// ====================================================
	public FrontlineHUD(Hero pHero) {
		this.loadJoystick(pHero);
		this.loadHUDBackground();
		this.loadUnitSelection();
		this.loadpauseButton();
		this.loadFoodBar();
		this.countdown();
		this.attachFrontlineHUD();
		this.increaseFoodBar();
		this.loadfightButton(pHero);

	}

	// ====================================================
	// GETTERS AND SETTERS
	// ====================================================

	// ====================================================
	// METHODES
	// ====================================================

	// sets this as the HUD for the Camera
	private void attachFrontlineHUD() {
		ResourceManager.getInstance().getCamera().setHUD(HUDRegion);
	}

	// Method that builds the Joystick and handles the Joystick Event
	private void loadJoystick(final Hero pHero) {

		mJoystick = new Joystick(JOYSTICK_MARGIN_LEFT,
				280 - JOYSTICK_MARGIN_BOTTOM, ResourceManager.getInstance()
						.getCamera(),
				ResourceManager.getInstance().JoystickBaseTextureRegion,
				ResourceManager.getInstance().JoystickKnobTextureRegion, 0.1f,
				200, ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager(),
				new IAnalogOnScreenControlListener() {

					@Override
					public void onControlChange(
							BaseOnScreenControl pBaseOnScreenControl, float x,
							float y) {

						pHero.move(x, y, pHero);
						/*if (x == 0 && y == 0) {
							pHero.action("STAND");
						} else {
							pHero.action("WALK");
						}
*/
					}

					@Override
					public void onControlClick(AnalogOnScreenControl arg0) {

					}

				});

		HUDRegion.setChildScene(mJoystick);
	}

	private void loadpauseButton() {

		pauseButton = new ButtonSprite(RIGHT_MARGIN - 50, 10,
				ResourceManager.getInstance().pauseHUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {

					// this.dettachFrontlineHUD();
					//System.out.println("sdfposdfjsodfsdopfjs");
					
					MainMenuLayer.getInstance().FadeIn();

				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
			}
		};
		HUDRegion.registerTouchArea(pauseButton);
		HUDRegion.attachChild(pauseButton);
	}
	
	private void loadfightButton(final Hero pHero) {

		fightButton = new ButtonSprite(RIGHT_MARGIN - 100, SELECTION_SPRITES_ROW_1_Y,
				ResourceManager.getInstance().fightHUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {

					// this.dettachFrontlineHUD();
					//System.out.println("sdfposdfjsodfsdopfjs");
					
					pHero.fight(1,1);

				}
				else pHero.fight(0, 0);
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
			}
		};
		HUDRegion.registerTouchArea(fightButton);
		HUDRegion.attachChild(fightButton);
	}

	// private Font font;
	private Text text;
	public int time = 0;
	public static String Zeit;

	// private Text text;

	public void countdown() {

		Thread timer = new Thread() {
			public void run() {
				Zeit = "0000";
				text = new Text(0, 0, MainActivity.font, Zeit,
						ResourceManager.getInstance().getEngine()
								.getVertexBufferObjectManager());
				HUDRegion.attachChild(text);
				text.setPosition(12, 12);
				while (true) {

					try {
						sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						time++;
						Zeit = String.valueOf(time);
						text.setText(Zeit);

					}
					// HUDRegion.reset();
					// HUDRegion.attachChild(text);

					/*
					 * if(HUDRegion.getChildCount() != 1){
					 * System.out.println(HUDRegion.getChildCount());
					 * HUDRegion.detachChild(text);
					 * System.out.println(HUDRegion.getChildCount());}
					 */
					// dosomething
				}
			}
		};

		timer.start();

	}
	
	public static String getTime(String Time){
		Time = Zeit;
		return Time;
	}

	private void loadFoodBar() {
		Rectangle OuterFoodBar = new Rectangle(200, 290,
				(float) (ResourceManager.getInstance().cameraWidth * 0.5), 40,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager());
		OuterFoodBar.setColor(0.4f, 0.2f, 0.3f);
		HUDRegion.attachChild(OuterFoodBar);

		InnerFoodBar = new Rectangle(210, 295,
				(float) (ResourceManager.getInstance().cameraWidth * 0.5) - 20,
				30, ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager());
		InnerFoodBar.setColor(0.9f, 0.6f, 0.3f);
		HUDRegion.attachChild(InnerFoodBar);
	}

	public static void decreaseFoodBar(int i) {
		if (InnerFoodBar.getWidth() > 0) {
			InnerFoodBar.setWidth(InnerFoodBar.getWidth() - i);
		}
	}

	public void increaseFoodBar() {
		Thread timer = new Thread() {
			public void run() {
				for (int i = 1; i > 0; i++) {

					try {
						sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						if (InnerFoodBar.getWidth() < 380) {
							System.out.println(InnerFoodBar.getWidth());
							InnerFoodBar.setWidth(InnerFoodBar.getWidth() + 30);
						}
					}
				}
			}
		};
		timer.start();
	}

	// Method that builds the area of the HUD where units are selected
	private void loadUnitSelection() {

		// Initialize SelectedUnit with the meaningless 0
		GameManager.getInstance().setSelectedUnit(0);

		Unit1SelectionSprite = new TiledSprite(SELECTION_SPRITES_COLUMN_1_X,
				SELECTION_SPRITES_ROW_1_Y, SELECTION_SPRITES_HEIGHT,
				SELECTION_SPRITES_WIDTH,
				ResourceManager.getInstance().mUnit1HUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					resetUnitSprite();
					Unit1SelectionSprite.setCurrentTileIndex(0);
					GameManager.getInstance().setSelectedUnit(1);
				}
				return true;
			}
		};
		Unit1SelectionSprite.setCurrentTileIndex(1);
		HUDRegion.registerTouchArea(Unit1SelectionSprite);
		HUDRegion.setTouchAreaBindingOnActionDownEnabled(true);
		HUDRegion.attachChild(Unit1SelectionSprite);

		Unit2SelectionSprite = new TiledSprite(SELECTION_SPRITES_COLUMN_1_X,
				SELECTION_SPRITES_ROW_2_Y, SELECTION_SPRITES_HEIGHT,
				SELECTION_SPRITES_WIDTH,
				ResourceManager.getInstance().mUnit2HUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					resetUnitSprite();
					Unit2SelectionSprite.setCurrentTileIndex(0);
					GameManager.getInstance().setSelectedUnit(2);
				}
				return true;
			}
		};
		Unit2SelectionSprite.setCurrentTileIndex(1);
		HUDRegion.registerTouchArea(Unit2SelectionSprite);
		HUDRegion.setTouchAreaBindingOnActionDownEnabled(true);
		HUDRegion.attachChild(Unit2SelectionSprite);

		Unit3SelectionSprite = new TiledSprite(SELECTION_SPRITES_COLUMN_2_X,
				SELECTION_SPRITES_ROW_1_Y, SELECTION_SPRITES_HEIGHT,
				SELECTION_SPRITES_WIDTH,
				ResourceManager.getInstance().mUnit3HUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					resetUnitSprite();
					Unit3SelectionSprite.setCurrentTileIndex(0);
					GameManager.getInstance().setSelectedUnit(3);
				}
				return true;
			}
		};
		Unit3SelectionSprite.setCurrentTileIndex(1);
		HUDRegion.registerTouchArea(Unit3SelectionSprite);
		HUDRegion.setTouchAreaBindingOnActionDownEnabled(true);
		HUDRegion.attachChild(Unit3SelectionSprite);

		Unit4SelectionSprite = new TiledSprite(SELECTION_SPRITES_COLUMN_2_X,
				SELECTION_SPRITES_ROW_2_Y, SELECTION_SPRITES_HEIGHT,
				SELECTION_SPRITES_WIDTH,
				ResourceManager.getInstance().mUnit4HUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					resetUnitSprite();
					Unit4SelectionSprite.setCurrentTileIndex(0);
					GameManager.getInstance().setSelectedUnit(4);
				}
				return true;
			}
		};
		Unit4SelectionSprite.setCurrentTileIndex(1);
		HUDRegion.registerTouchArea(Unit4SelectionSprite);
		HUDRegion.setTouchAreaBindingOnActionDownEnabled(true);
		HUDRegion.attachChild(Unit4SelectionSprite);

		Unit5SelectionSprite = new TiledSprite(SELECTION_SPRITES_COLUMN_3_X,
				SELECTION_SPRITES_ROW_1_Y, SELECTION_SPRITES_HEIGHT,
				SELECTION_SPRITES_WIDTH,
				ResourceManager.getInstance().mUnit5HUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					resetUnitSprite();
					Unit5SelectionSprite.setCurrentTileIndex(0);
					GameManager.getInstance().setSelectedUnit(5);
				}
				return true;
			}
		};
		Unit5SelectionSprite.setCurrentTileIndex(1);
		HUDRegion.registerTouchArea(Unit5SelectionSprite);
		HUDRegion.setTouchAreaBindingOnActionDownEnabled(true);
		HUDRegion.attachChild(Unit5SelectionSprite);

		Unit6SelectionSprite = new TiledSprite(SELECTION_SPRITES_COLUMN_3_X,
				SELECTION_SPRITES_ROW_2_Y, SELECTION_SPRITES_HEIGHT,
				SELECTION_SPRITES_WIDTH,
				ResourceManager.getInstance().mUnit6HUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					resetUnitSprite();
					Unit6SelectionSprite.setCurrentTileIndex(0);
					GameManager.getInstance().setSelectedUnit(6);
				}
				return true;
			}
		};
		Unit6SelectionSprite.setCurrentTileIndex(1);
		HUDRegion.registerTouchArea(Unit6SelectionSprite);
		HUDRegion.setTouchAreaBindingOnActionDownEnabled(true);
		HUDRegion.attachChild(Unit6SelectionSprite);

	}

	// Method that resets the currently selected UnitSelectionSprite to its
	// unselected tile
	private void resetUnitSprite() {
		switch (GameManager.getInstance().getSelectedUnit()) {
		case 0:
			GameManager.getInstance().setSelectedUnit(3);
			break;

		case 1:
			Unit1SelectionSprite.setCurrentTileIndex(1);
			// GameManager.getInstance().setSelectedUnit(1);
		case 2:
			Unit2SelectionSprite.setCurrentTileIndex(1);
			// GameManager.getInstance().setSelectedUnit(2);
		case 3:
			Unit3SelectionSprite.setCurrentTileIndex(1);
			// GameManager.getInstance().setSelectedUnit(3);
		case 4:
			Unit4SelectionSprite.setCurrentTileIndex(1);
			// GameManager.getInstance().setSelectedUnit(1);
		case 5:
			Unit5SelectionSprite.setCurrentTileIndex(1);
			// GameManager.getInstance().setSelectedUnit(1);
		case 6:
			Unit6SelectionSprite.setCurrentTileIndex(1);
			// GameManager.getInstance().setSelectedUnit(1);
		}
	}

	// Method that makes the HUD beautiful:)
	private void loadHUDBackground() {

		Rectangle blabla = new Rectangle(0, HUD_TOP_BORDER,
				(float) (ResourceManager.getInstance().cameraWidth),
				ResourceManager.getInstance().cameraHeight - HUD_TOP_BORDER,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager());
		HUDRegion.attachChild(blabla);
		blabla.setColor(1f, 0.5f, 0.5f);

	}
	/*
	 * private void loadReplayButton() {
	 * 
	 * pauseButton = new ButtonSprite(RIGHT_MARGIN - 50, 10,
	 * ResourceManager.getInstance().pauseHUDTextureRegion,
	 * ResourceManager.getInstance().getEngine()
	 * .getVertexBufferObjectManager()) {
	 * 
	 * @Override public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
	 * final float pTouchAreaLocalX, final float pTouchAreaLocalY) { if
	 * (pSceneTouchEvent.isActionDown()) {
	 * 
	 * //this.dettachFrontlineHUD(); System.out.println("JAAAAUUUUUSN");
	 * attachFrontlineHUD();
	 * 
	 * 
	 * } return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
	 * pTouchAreaLocalY); } }; MenuRegion.registerTouchArea(pauseButton);
	 * MenuRegion.attachChild(pauseButton); }
	 */
}
